package com.caigou.modules.query.ui.coldhot

import android.os.Bundle
import android.text.TextUtils
import androidx.activity.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.constants.AppConstants
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseActivity
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.modules.query.adpter.ColdHotDetailItemAdapter
import com.caigou.modules.query.bean.ColdHotDetailItemBean
import com.caigou.modules.query.databinding.ActivityColdHotDetailBinding
import com.caigou.modules.query.viewmodel.ColdHotViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2022/01/04
 */
@Route(path = RouterPath.Query.PAGE_COLD_HOT_DETAIL)
@AndroidEntryPoint
class ColdHotDetailActivity : BaseActivity() {

    private lateinit var binding: ActivityColdHotDetailBinding

    private val coldHotViewModel: ColdHotViewModel by viewModels()

    @JvmField
    @Autowired(name = AppConstants.COLD_HOT_COUNT)
    var count = 10

    @JvmField
    @Autowired(name = AppConstants.COLD_HOT_NUMBER)
    var number = ""

    private lateinit var mAdapter: ColdHotDetailItemAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityColdHotDetailBinding.inflate(layoutInflater)
        setContentView(binding.root)
        containerId = binding.coldHotDetailLlContainer.id
        initView()
    }

    private fun initView() {
        ARouter.getInstance().inject(this)


        mAdapter = ColdHotDetailItemAdapter(number, mutableListOf())

        binding.coldHotDetailRecyclerView.run {
            layoutManager = LinearLayoutManager(this@ColdHotDetailActivity)
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(SpaceItemDecoration(this@ColdHotDetailActivity))
        }
        mAdapter.setOnItemClickListener { adapter, view, position ->

        }

        binding.coldHotDetailTvBack.setOnClickListener {
            finish()
        }
        fetchColdHotDetailList()
    }

    override fun observeViewModel() {
        super.observeViewModel()
        observe(coldHotViewModel.coldHotDetailListLiveData, ::handleGetColdHotDetailList)
    }

    private fun handleGetColdHotDetailList(result: Resource<MutableList<ColdHotDetailItemBean>>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取冷热号码异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                result.data?.apply {
//                    mAdapter.setList(this)
                    mAdapter.setList(this)
                }
            }
        }
    }

    private fun fetchColdHotDetailList() {
        coldHotViewModel.getColdHotDetailList(count)
    }
}